package com.lagou.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import static com.lagou.constant.RabbitMqConstant.*;

/**
 * @Author: xunliu
 * @Date: 2021/01/24 11:02
 */
@Configuration
public class RabbitMqConfig {

    // 延迟支付队列
    @Bean
    public Queue payQueue() {
        Map<String, Object> props = new HashMap<>(){{
            put("x-message-ttl", 5_000);
            put("x-dead-letter-exchange", PAY_FAIL_EXCHANGE);
            put("x-dead-letter-routing-key", PAY_FAIL_ROUTING_KEY);
        }};
        final Queue queue = new Queue(PAY_QUEUE, true, false, false, props);
        return queue;
    }

    @Bean
    public Exchange payExchange() {
        return new DirectExchange(PAY_EXCHANGE, true, false, null);
    }

    @Bean
    @Autowired
    public Binding payQueueExBinding(Queue payQueue, Exchange payExchange) {
        return BindingBuilder.bind(payQueue).to(payExchange).with(PAY_ROUTING_KEY).noargs();
    }

    // 延迟支付死信队列
    @Bean
    public Queue payFailQueue() {
        final Queue queue = new Queue(PAY_FAIL_QUEUE, true, false, false, null);
        return queue;
    }

    @Bean
    public Exchange payFailExchange() {
        return new DirectExchange(PAY_FAIL_EXCHANGE, true, false, null);
    }

    @Bean
    @Autowired
    public Binding payFailQueueExBinding(Queue payFailQueue, Exchange payFailExchange) {
        return BindingBuilder.bind(payFailQueue).to(payFailExchange).with(PAY_FAIL_ROUTING_KEY).noargs();
    }

}
